package com.xb.p44_dragdemo;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Color;
import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    static final String IMAGE_TAG="向目标区域移动中...";
    ImageView imageView;
    LinearLayout container;
    RelativeLayout topContainer;
    TextView tittle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView=findViewById(R.id.image);
        imageView.setTag(IMAGE_TAG);

        container=findViewById(R.id.container);
        topContainer=findViewById(R.id.topContainer);
        tittle=findViewById(R.id.tittle);

        imageView.setOnLongClickListener(new myLongClick());
        topContainer.setOnDragListener(new myTopDrag());
        container.setOnDragListener(new myBottomDrag());
    }

    public class myLongClick implements View.OnLongClickListener {

        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public boolean onLongClick(View v) {
            ClipData.Item item=new ClipData.Item((String)v.getTag());
            ClipData data=new ClipData(IMAGE_TAG,new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN},item);

            //开始拖放
            v.startDragAndDrop(data,new View.DragShadowBuilder(v),null,0);
            return true;
        }
    }

    public class myTopDrag implements View.OnDragListener {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action=event.getAction();

            switch (action){
                case DragEvent.ACTION_DRAG_STARTED:
                    return true;
                case DragEvent.ACTION_DRAG_ENTERED:
                    return true;
                case DragEvent.ACTION_DRAG_LOCATION:
                    return true;
                case DragEvent.ACTION_DRAG_EXITED:
                    return true;
                case DragEvent.ACTION_DROP:
                    imageView.setX(event.getX()-imageView.getWidth()/2);
                    imageView.setY(event.getY()-imageView.getHeight()/2);
                    return true;
                case DragEvent.ACTION_DRAG_ENDED:
                    return true;
            }
            return false;
        }
    }

    public class myBottomDrag implements View.OnDragListener{

        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action=event.getAction();

            switch (action){
                case DragEvent.ACTION_DRAG_STARTED:
                    if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
                        return true;
                    else
                        return false;
                case DragEvent.ACTION_DRAG_ENTERED:
                    container.setBackgroundColor(Color.YELLOW);
                    return true;
                case DragEvent.ACTION_DRAG_LOCATION:
                    return true;
                case DragEvent.ACTION_DRAG_EXITED:
                    container.setBackgroundColor(Color.BLUE);
                    tittle.setText("");
                    return true;
                case DragEvent.ACTION_DROP:
                    ClipData.Item item=event.getClipData().getItemAt(0);
                    String data=item.getText().toString();
                    tittle.setText(data+"-->("+event.getX()+","+event.getY()+")");
                    return true;
                case DragEvent.ACTION_DRAG_ENDED:
                    return true;
            }
            return false;
        }
    }
}